package com.ruoyi.finance.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.finance.domain.AccountsReceivable;
import com.ruoyi.finance.domain.BatchBilling;
import com.ruoyi.finance.mapper.BatchBillingMapper;
import com.ruoyi.finance.service.IAccountsReceivableService;
import com.ruoyi.finance.service.IBatchBillingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 分批计费(应收)Service业务层处理
 * 
 * @author liuyu
 * @date 2024-10-30
 */
@Service
public class BatchBillingServiceImpl implements IBatchBillingService 
{
    @Autowired
    private BatchBillingMapper batchBillingMapper;

    @Resource
    private IAccountsReceivableService accountsReceivableService;

    /**
     * 查询分批计费(应收)
     * 
     * @param id 分批计费(应收)主键
     * @return 分批计费(应收)
     */
    @Override
    public List<BatchBilling> selectBatchBillingById(Long id)
    {
        return batchBillingMapper.selectBatchBillingById(id);
    }

    /**
     * 新增分批计费(应收)
     * 
     * @param batchBilling 分批计费(应收)
     * @return 结果
     */
    @Override
    public int insertBatchBilling(BatchBilling batchBilling)
    {

        //已收款总金额
        BigDecimal bigDecimal = selectSumAmount(batchBilling.getAccountsReceivableId()) == null ? BigDecimal.valueOf(0) : selectSumAmount(batchBilling.getAccountsReceivableId());
        //应收款金额
        AccountsReceivable accountsReceivable = accountsReceivableService.selectAccountsReceivableById(batchBilling.getAccountsReceivableId());

        if (bigDecimal.add(batchBilling.getCollectionAmount()).compareTo(accountsReceivable.getAmount()) > 0 ){
            throw new ServiceException("总收款金额超过应收金额");
        }
        batchBillingMapper.insertBatchBilling(batchBilling);

        if (bigDecimal.compareTo(accountsReceivable.getAmount()) < 0) {
            AccountsReceivable accountsReceivable1 = new AccountsReceivable();
            accountsReceivable1.setId(batchBilling.getAccountsReceivableId());
            accountsReceivable1.setStatus(2L);
            accountsReceivableService.updateAccountsReceivable(accountsReceivable1);
        }
            updateStatus(batchBilling);

        return 1;
    }

    /**
     * 修改分批计费(应收)
     * 
     * @param batchBilling 分批计费(应收)
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBatchBilling(BatchBilling batchBilling)
    {

        //已收款总金额
        BigDecimal bigDecimal = selectSumAmount(batchBilling.getAccountsReceivableId()) == null ? BigDecimal.valueOf(0) : selectSumAmount(batchBilling.getAccountsReceivableId());
        //应收款金额
        AccountsReceivable accountsReceivable = accountsReceivableService.selectAccountsReceivableById(batchBilling.getAccountsReceivableId());

        if (bigDecimal.add(batchBilling.getCollectionAmount()).compareTo(accountsReceivable.getAmount()) > 0 ){
            throw new ServiceException("总收款金额超过应收金额");
        }
        //进行更改收款记录操作
        batchBillingMapper.updateBatchBilling(batchBilling);
        updateStatus(batchBilling);
        return 1;
    }

    @Override
    public BigDecimal selectSumAmount(Long accountsReceivableId) {
        return batchBillingMapper.selectSumAmount(accountsReceivableId);
    }

    @Override
    public BatchBilling getBillingById(Long id) {
        return batchBillingMapper.getBillingById(id);
    }

    public void updateStatus(BatchBilling batchBilling){
        //已收款总金额
        BigDecimal bigDecimal = selectSumAmount(batchBilling.getAccountsReceivableId());
        //应收款金额
        AccountsReceivable accountsReceivable = accountsReceivableService.selectAccountsReceivableById(batchBilling.getAccountsReceivableId());
        if (bigDecimal.compareTo(accountsReceivable.getAmount()) >= 0 && accountsReceivable.getStatus() != 1){
            //对状态进行修改
            AccountsReceivable accountsReceivable1 = new AccountsReceivable();
            accountsReceivable1.setStatus(1L);
            accountsReceivable1.setId(accountsReceivable.getId());
            accountsReceivableService.updateAccountsReceivable(accountsReceivable1);
        }
    }
}
